package cn.itcast.solr;

import cn.itcast.domain.Item;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;

import java.util.List;

public class SolrIndexQuery {
    /**
     * @Description:创建一个查询的方法
     *
     * @Author:fsy
     * @Date:2018/3/2
     */
    @Test
    public void testQueryIndex() throws SolrServerException {
        //连接服务器
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core1");
        //创建查询对象
        SolrQuery solrQuery = new SolrQuery("title:手机");
        //查询请求发出后，得到相应
        QueryResponse response = solrServer.query(solrQuery);
        //从响应中提取响应的数据（得到结果）
        SolrDocumentList results = response.getResults();
        for (SolrDocument solrDocument : results) {
            String id = (String) solrDocument.get("id");
            String title = (String) solrDocument.get("title");
            Long price = (Long) solrDocument.get("price");
            System.out.println("id" + id + "title" + title + "price" + price);
        }
        System.out.println("创建查询的方法完成");
    }

    /**
     * @Description:通过实体类进行创建一个查询方法
     *
     * @Author:fsy
     * @Date:2018/3/2
     */
    @Test
    public void testQueryIndexWithBean() throws SolrServerException {
        //创建服务器
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core1");
        //创建查询对象
        SolrQuery solrQuery = new SolrQuery("title:手机");
        //查询请求发出后，得到相应
        QueryResponse response = solrServer.query(solrQuery);

        List<Item> items = response.getBeans(Item.class);
        for (Item item : items) {
            System.out.println(item);
        }
        System.out.println("创建实体类查询方法完成");
    }

    /**
     * @Description:范围查询
     *
     * @Author:fsy
     * @Date:2018/3/2
     */
    @Test
    public void testQueryIndexRange() throws SolrServerException {
        //创建服务器
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core1");
        //创建查询对象    格式，字段名 冒号 中括号 start TO end
        SolrQuery solrQuery = new SolrQuery("price:[300000 TO 400000]");
        //查询请求发出后，得到相应
        QueryResponse response = solrServer.query(solrQuery);

        List<Item> items = response.getBeans(Item.class);
        for (Item item : items) {
            System.out.println(item);
        }
        System.out.println("创建范围查询完成");

    }

    /**
     * @Description:布尔查询
     *
     * @Author:fsy
     * @Date:2018/3/2
     */
    @Test
    public void testBooleanIndex() throws SolrServerException {
        //创建服务器
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core1");
        //创建查询条件
        SolrQuery solrQuery = new SolrQuery("title:小米 OR title:iphone");
        //执行查询条件
        QueryResponse response = solrServer.query(solrQuery);

        List<Item> items = response.getBeans(Item.class);
        for (Item item : items) {
            System.out.println(item);
        }
        System.out.println("布尔查询完成");
    }

    /**
     * @Description:相似度查询
     *
     * @Author:fsy
     * @Date:2018/3/2
     */
    @Test
    public void testSimilarIndex() throws SolrServerException {
        //创建服务器
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core1");
        //相似度查询与Lucene中的FuzzyQuery本质相同，允许编辑距离为0~2
        SolrQuery solrQuery = new SolrQuery("title:iphcna~2");
        //执行查询条件
        QueryResponse response = solrServer.query(solrQuery);
        List<Item> items = response.getBeans(Item.class);
        for (Item item : items) {
            System.out.println(item);
        }
        System.out.println("相似度查询完成");
    }
}
